﻿Public Class Frm_Massarif_khibra
    Public dv As Dossier
    Public avo As Khibra
    Public dep As Depenses_khibra

    Public Sub SumDepences()
        Dim s As Double = 0
        Dim vbar As Double = 0
        For Each khib As Depenses_khibra In avo.List_depences_khibra
            vbar = khib.Montant
            If khib.type = "المسترجع" Then
                vbar = -vbar
            End If
            s = s + vbar
        Next
            txt_total.Text = s
    End Sub

    Private Sub Btn_rech_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_rech.Click

        If Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر المرجع أولا", MsgBoxStyle.Critical, "خطأ في الإضافة")
        Else
            If comb_num_khibra.SelectedIndex = -1 Then
                MsgBox("هذا الملف ليس له خبرة", MsgBoxStyle.Critical, "خطأ")
            Else
                Me.Enabled = False
                Frm_Rech_Massarif_khibra.Show()
            End If
        End If
    End Sub

    Private Sub Frm_Massarif_khibra_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Comb_type.SelectedIndex = 0
        Partager.close()
        Partager.nonInitial = New ArrayList
        Partager.nonInitial.Add("Comb_marji3ona")
        Partager.remplirDossier(Comb_marji3ona)
    End Sub

    Private Sub Btn_ajout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_ajout.Click
        If (List_tous.SelectedIndices.Count > 0) Then
            MsgBox("عفوا لا يمكنك الإضافة، لإضافة مصاريف جديدة عليك الضغط على الزر جديد ثم إدخال البيانات", MsgBoxStyle.Critical, "خطأ في الإضافة")
        ElseIf Comb_marji3ona.SelectedIndex < 0 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ")
        ElseIf comb_num_khibra.SelectedIndex < 0 Then
            MsgBox("هذا الملف ليس له خبرة", MsgBoxStyle.Critical, "خطأ")
        Else
            If txt_num_wasl.Text <> "" And txt_mablagh.Text <> "" And txt_num_hissab.Text <> "" Then
                If MsgBox("هل تريد فعلا إضافة هذه المصاريف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "إضافة مصاريف") = MsgBoxResult.Yes Then
                    Partager.close()
                    Try

                        Dim cli As New Depenses_khibra(1, txt_num_hissab.Text, txt_num_wasl.Text, txt_mablagh.Text, Comb_type.Text, Date_masarif.Value, txt_molahadat.Text)
                        avo.Ajouter_depences_khibra(cli)

                        List_tous.Items.Clear()

                        For Each khib As Depenses_khibra In avo.List_depences_khibra
                            List_tous.Items.Add(New ListViewItem(New String() {khib.Num_compt, khib.Num_recus, khib.Montant, khib.type, khib.Date_depence}))
                        Next
                        MsgBox("تمت الإضافة بنجاح", MsgBoxStyle.Information, "نجاح")
                        Partager.vider(Me, Partager.nonInitial)
                        SumDepences()
                    Catch ex As Exception
                        MsgBox(ex.Message)
                    End Try

                End If
            Else
                MsgBox("أدخل المعلومات من فضلك", MsgBoxStyle.Critical, "خطأ")
            End If
        End If
    End Sub

    Private Sub Btn_modif_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_modif.Click
        If (List_tous.SelectedIndices.Count <= 0) Then
            MsgBox("عفوا لا يمكنك التعديل، اختر مصاريف من القائمة", MsgBoxStyle.Critical, "خطأ في التعديل")
        ElseIf Comb_marji3ona.SelectedIndex = -1 Then
            MsgBox("اختر الملف أولا", MsgBoxStyle.Critical, "خطأ في التعديل")
        ElseIf comb_num_khibra.SelectedIndex < 0 Then
            MsgBox("هذا الملف ليس له خبرة", MsgBoxStyle.Critical, "خطأ")
        Else
            Try
                If MsgBox("هل تريد فعلا تعديل هذه المصاريف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "تعديل إجراء") = MsgBoxResult.Yes Then
                    Partager.close()
                    Dim cli As New Depenses_khibra(1, txt_num_hissab.Text, txt_num_wasl.Text, txt_mablagh.Text, Comb_type.Text, Date_masarif.Value, txt_molahadat.Text)
                    avo.modifier_depences_khibra(dep.Id, cli)

                    List_tous.Items.Clear()
                    For Each khib As Depenses_khibra In avo.List_depences_khibra
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Num_compt, khib.Num_recus, khib.Montant, khib.type, khib.Date_depence}))
                    Next

                    MsgBox("تمت تعديل المصاريف بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me, Partager.nonInitial)
                    SumDepences()
                End If
            Catch ex As Exception

                MsgBox(ex.Message)

            End Try
        End If
    End Sub

    Private Sub Btn_supp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_supp.Click
        If List_tous.SelectedIndices.Count > 0 Then
            If MsgBox("هل تريد فعلا حذف هذه المصاريف ؟", MsgBoxStyle.YesNo Or MsgBoxStyle.Information, "حذف مصاريف") = MsgBoxResult.Yes Then
                Partager.close()
                Try
                    Dim clt As Depenses_khibra = avo.List_depences_khibra(List_tous.SelectedIndices.Item(0))
                    avo.suprimmer_depences_khibra(dep.Id)

                    List_tous.Items.Clear()
                    For Each khib As Depenses_khibra In avo.List_depences_khibra
                        List_tous.Items.Add(New ListViewItem(New String() {khib.Num_compt.ToString, khib.Num_recus, khib.Montant, khib.type, khib.Date_depence}))
                    Next
                    MsgBox("عملية الحذف تمت بنجاح", MsgBoxStyle.Information, "نجاح")
                    Partager.vider(Me, Partager.nonInitial)
                    SumDepences()
                Catch ex As Exception
                    MsgBox(ex.Message)
                End Try
            End If
        Else
            MsgBox("اختر مصاريف من القائمة", MsgBoxStyle.Information, "خطأ")
        End If
    End Sub

    Private Sub List_tous_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles List_tous.SelectedIndexChanged
        Try
            For Each i As Integer In List_tous.SelectedIndices

                dep = avo.List_depences_khibra(i)

                txt_num_hissab.Text = dep.Num_compt
                txt_num_wasl.Text = dep.Num_recus
                txt_mablagh.Text = dep.Montant
                Comb_type.Text = dep.type
                Date_masarif.Value = dep.Date_depence
                txt_molahadat.Text = dep.Remarque
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        If List_tous.SelectedIndices.Count < 1 Then
            Partager.vider(Me, Partager.nonInitial)
            SumDepences()
        End If

    End Sub

    Private Sub comb_num_khibra_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comb_num_khibra.SelectedIndexChanged
        Try
            For Each d As Khibra In dv.List_khibra
                If d.Num = comb_num_khibra.Text Then
                    avo = d
                    avo.remplir_list_depences_khibra()

                End If
            Next
            List_tous.Items.Clear()
            For Each khib As Depenses_khibra In avo.List_depences_khibra
                List_tous.Items.Add(New ListViewItem(New String() {khib.Num_compt.ToString, khib.Num_recus, khib.Montant, khib.type, khib.Date_depence}))
            Next
            SumDepences()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Btn_nouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_nouv.Click
        Partager.vider(Me, Partager.nonInitial)
        txt_num_hissab.Select()
    End Sub

    Private Sub txt_mablagh_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt_mablagh.KeyPress
        Dim allowedChars As String = "1234567890,"
        If e.KeyChar = ChrW(8) Then
            e.Handled = False
        ElseIf allowedChars.IndexOf(e.KeyChar) = -1 Then
            ' Invalid Character
            e.Handled = True
        End If
    End Sub

    Private Sub Comb_marji3ona_SelectedIndexChanged_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Comb_marji3ona.SelectedIndexChanged
        Partager.vider(Me, Partager.nonInitial)
        Try
            Partager.deClt = DirectCast(Comb_marji3ona.SelectedItem, DictionaryEntry)
            For Each d As Dossier In Partager.moi.List_info_fichier
                If d.id = CInt(Partager.deClt.Key) Then
                    dv = d
                    dv.remplir_List_khibra()
                End If
            Next
            List_tous.Items.Clear()
            comb_num_khibra.Items.Clear()
            For Each khib As Khibra In dv.List_khibra
                comb_num_khibra.Items.Add(khib.Num)
            Next
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        If comb_num_khibra.Items.Count > 0 Then
            comb_num_khibra.SelectedIndex = 0
        End If
    End Sub

    Private Sub Btn_Rech_Doss_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Rech_Doss.Click

        Me.Enabled = False
        Partager.frmafect = Me
        Frm_Recherche.Show()
        Partager.type = Me.Name

    End Sub
End Class